package net.onecloud.onecloud.base;

import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.TextView;

import net.onecloud.onecloud.R;
import net.onecloud.onecloud.utils.ToolUtils;


/**
 * Created by zaizai on 16/6/14.
 */
public abstract class BaseAppCompatActivity extends AppCompatActivity implements View.OnClickListener {

    /**
     * Log tag
     */
    protected static String TAG_LOG = null;
    /**
     * 屏幕参数
     */
    protected int mScreenWidth = 0;
    protected int mScreenHeight = 0;
    protected float mScreenDensity = 0.0f;

    /**
     * 上下文
     */
    protected Context mContext = null;
    private Toolbar mToolbar;
    private ImageButton mLeftButton;
    private ImageButton mRightButton;
    private TextView mToolBarTitle;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mContext = this;//上下文对象
        TAG_LOG = this.getClass().getSimpleName();//打印log


    }

    @Override
    public void setContentView(@LayoutRes int layoutResID) {
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
        super.setContentView(layoutResID);
        mToolbar = (Toolbar) this.findViewById(R.id.m_toolbar);
        ToolUtils.clearStateBar(mToolbar, this);
        mLeftButton = (ImageButton) findViewById(R.id.m_left_button);
        mRightButton = (ImageButton) findViewById(R.id.m_right_button);
        mToolBarTitle = (TextView) findViewById(R.id.m_toolbar_title);
        mRightButton.setOnClickListener(this);
        mLeftButton.setOnClickListener(this);
        mToolBarTitle.setText("");

    }

    public void setToolBarTitle(String title) {
        mToolBarTitle.setText(title);

    }

    public void HideLeftButton(boolean isHide) {
        if (isHide) {
            mLeftButton.setVisibility(View.GONE);
            return;
        }
        mLeftButton.setVisibility(View.VISIBLE);
    }

    public void HideToolBar(boolean isHide) {
        if (isHide) {
            mToolbar.setVisibility(View.GONE);
            return;
        }
        mToolbar.setVisibility(View.VISIBLE);
    }

    public void HideRightButton(boolean isHide) {
        if (isHide) {
            mRightButton.setVisibility(View.GONE);
            return;
        }
        mRightButton.setVisibility(View.VISIBLE);
    }

    public void onClickLeftButton() {
        finish();
    }

    public void onClickRightButton() {
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.m_left_button: {
                onClickLeftButton();
                break;
            }
            case R.id.m_right_button: {
                onClickRightButton();
                break;
            }

        }
    }

    protected void showHit(String message) {
        new AlertDialog.Builder(this)
                .setTitle("提示")
                .setMessage(message)
                .setPositiveButton("确定", null)
                .show();
    }

    protected void showHitAndFinish(String message) {
        new AlertDialog.Builder(this)
                .setTitle("提示")
                .setMessage(message)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        BaseAppCompatActivity.this.finish();
                    }
                }).setNegativeButton("取消", null)
                .show();
    }

    protected void showHitMustFinish(String message) {
        new AlertDialog.Builder(this)
                .setTitle("提示")
                .setMessage(message)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        BaseAppCompatActivity.this.finish();
                    }
                })
                .show();
    }
}
